package com.guanghua.brick.db;

import java.sql.Connection;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import com.guanghua.brick.BrickConstant;
import com.guanghua.brick.util.IConfig;

/**
 * 通过JNDI获取数据库连接
 * @author Administrator
 */
public class JNDISQLConnection implements ISQLConnection {

	public Connection createConnection(String jndi) throws Exception {
		return createDataSource(jndi).getConnection();
	}

	public DataSource createDataSource(String jndi) throws Exception {
		//获取jndi值，默认的或者传入的
		IConfig config = BrickConstant.APP_DB_CONN_CONFIG;
		String defaultJndi = config.getAttribute("default-jndi");
		if (jndi == null) jndi = defaultJndi;
		
		//寻找jndi
		Context ctx = new InitialContext();
		return (DataSource)ctx.lookup(jndi);
	}

}
